package com.fxc.crowd.mvc.handler;

import com.fxc.crowd.constant.CrowdConstant;
import com.fxc.crowd.entity.po.AdminPO;
import com.fxc.crowd.service.api.AdminService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;

@Controller
public class AdminHandler {

    @Autowired
    private AdminService adminService;


    @RequestMapping("/admin/update.html")
    public String update(AdminPO AdminPO, @RequestParam("pageNum") Integer pageNum, @RequestParam("keyword") String keyword) {

        adminService.update(AdminPO);

        return "redirect:/admin/get/page.html?pageNum=" + pageNum + "&keyword=" + keyword;
    }

    @RequestMapping("/admin/to/edit/page.html")
    public String toEditPage(@RequestParam("adminId") Integer adminId, ModelMap modelMap) {

        // 1.根据adminId查询admin对象
        AdminPO adminPO = adminService.getAdminById(adminId);

        // 2.将adminPO对象存入模型
        modelMap.addAttribute("admin", adminPO);

        return "admin-edit";
    }


    @PreAuthorize("hasAuthority('user:save')")
    @RequestMapping("/admin/save.html")
    public String save(AdminPO adminPO) {

        adminService.saveAdmin(adminPO);

        return "redirect:/admin/get/page.html?pageNum=" + Integer.MAX_VALUE;
    }


    @RequestMapping("/admin/remove/{adminId}/{pageNum}/{keyword}.html")
    public String remove(
            @PathVariable("adminId") Integer adminId,
            @PathVariable("pageNum") Integer pageNum,
            @PathVariable("keyword") Integer keyword
    ) {

        // 执行删除
        adminService.remove(adminId);

        //尝试方案1 页面跳转；回到分页页面
        //return "admin-page";

        // 尝试方案2：转发到/admin/get/page.html地址，一旦刷新页面会重复执行浪费性能
        //return "forward:/admin/get/page.html";

        // 尝试方案3：重定向
        // 同时为了保存原本所在的页面和查询关键词在附加pageNum和keyword两个参数
        return "redirect:/admin/get/page.html?pageNum=" + pageNum + "&keyword=" + keyword;

    }


    @RequestMapping("/admin/get/page.html")
    public String getPageInfo(
            // 使用@RequestParam注解的defaultValue属性，指定默认值，在请求中没后携带对应参数时使用默认值
            // keyword默认值使用空字符串，和sql语句配合实现两种情况适配
            @RequestParam(value = "keyword", defaultValue = "") String keyword,
            // pageNum默认值使用1
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            // PageNum默认值使用5
            @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
            ModelMap modelMap) {

        // 调用Service方法获取PageInfo对象
        PageInfo<AdminPO> pageInfo = adminService.getPageInfo(keyword, pageNum, pageSize);
        //System.out.println("pageInfo=" + pageInfo);

        // 将pageInfo对象存入模型pageInfo
        modelMap.addAttribute(CrowdConstant.ATTR_NAME_PAGE_INFO, pageInfo);
        return "admin-page";
    }

    @RequestMapping("/admin/do/logout.html")
    public String doLogOut(HttpSession session) {
        // 强制session失效
        session.invalidate();

        return "redirect:/admin/to/login/page.html";
    }

    @RequestMapping("/admin/do/login.html")
    public String doLogin(
            @RequestParam("loginAcct") String loginAcct,
            @RequestParam("userPswd") String userPswd,
            HttpSession session
    ) {
        // 调用Service方法执行登录检查
        // 返回admin对象则说明登录成功，失败就抛出异常
        AdminPO adminPO = adminService.getAdminByLoginAcct(loginAcct, userPswd);

        // 将登录成功返回Admin对象存入Session域
        session.setAttribute(CrowdConstant.ATTR_NAME_LOGIN_ADMIN, adminPO);

        return "redirect:/admin/to/main/page.html";
    }
}
